package com.jia.train.seat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jia.train.global.enums.BusinessEnum;
import com.jia.train.global.exception.BusinessException;
import com.jia.train.global.page.PageResponse;
import com.jia.train.global.utils.SnowIdUtil;
import com.jia.train.seat.entity.TrainCarriage;
import com.jia.train.seat.entity.TrainCarriageExample;
import com.jia.train.seat.enums.SeatColEnum;
import com.jia.train.seat.mapper.TrainCarriageMapper;
import com.jia.train.seat.request.TrainCarriageQueryRequest;
import com.jia.train.seat.request.TrainCarriageSaveRequest;
import com.jia.train.seat.response.TrainCarriageQueryResponse;
import com.jia.train.seat.response.TrainCarriageSaveResponse;
import com.jia.train.seat.service.TrainCarriageService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class TrainCarriageServiceImpl implements TrainCarriageService {

    @Resource
    private TrainCarriageMapper trainCarriageMapper;

    @Override
    public TrainCarriageSaveResponse insert(TrainCarriageSaveRequest request) {
        TrainCarriage trainCarriage = BeanUtil.copyProperties(request, TrainCarriage.class);
        DateTime now = DateTime.now();

        String seatType = request.getSeatType();
        List<SeatColEnum> colsByType = SeatColEnum.getColsByType(seatType);
        int size = colsByType.size();
        String tripCode = request.getTripCode();
        Integer index = request.getIndex();

        if (ObjectUtil.isEmpty(trainCarriage.getId())) {
            if (getTrainCarriageCount(tripCode, index) > 0) {
                throw new BusinessException(BusinessEnum.CARRIAGE_NUMBER_OF_THE_SAME_TRAIN_ALREADY_EXISTS);
            }
            trainCarriage.setId(SnowIdUtil.getLongSnowNextId());
            trainCarriage.setCreateTime(now);
            trainCarriage.setColCount(size);
            trainCarriage.setSeatCount(trainCarriage.getRowCount() * size);
            trainCarriageMapper.insert(trainCarriage);
        } else {
            trainCarriage.setUpdateTime(now);
            trainCarriageMapper.updateByPrimaryKeySelective(trainCarriage);
        }

        return BeanUtil.copyProperties(trainCarriage, TrainCarriageSaveResponse.class);
    }

    @Override
    public PageResponse<TrainCarriageQueryResponse> selectPage(TrainCarriageQueryRequest request) {

        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        // 排序
        trainCarriageExample.setOrderByClause("trip_code asc,`index` asc");
        // 条件
        TrainCarriageExample.Criteria criteria = trainCarriageExample.createCriteria();
        String tripCode = request.getTripCode();
        if (StringUtils.hasText(tripCode)) {
            criteria.andTripCodeEqualTo(tripCode);
        }
        // 构造分页
        PageHelper.startPage(request.getPage(), request.getSize());
        List<TrainCarriage> TrainCarriages = trainCarriageMapper.selectByExample(trainCarriageExample);
        PageInfo<TrainCarriage> pageInfo = new PageInfo<>(TrainCarriages);

        List<TrainCarriageQueryResponse> list
                = BeanUtil.copyToList(TrainCarriages, TrainCarriageQueryResponse.class);

        PageResponse<TrainCarriageQueryResponse> pageResponse = new PageResponse<>();
        pageResponse.setTotal(pageInfo.getTotal());
        pageResponse.setList(list);
        return pageResponse;
    }

    @Override
    public void deleteTrainCarriage(Long id) {
        trainCarriageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TrainCarriage> selectAllByTripCode(String tripCode) {
        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        trainCarriageExample.setOrderByClause("`index` asc");
        trainCarriageExample.createCriteria().andTripCodeEqualTo(tripCode);
        return trainCarriageMapper.selectByExample(trainCarriageExample);
    }

    private long getTrainCarriageCount(String code, Integer index) {
        TrainCarriageExample TrainCarriageExample = new TrainCarriageExample();
        TrainCarriageExample.Criteria criteria = TrainCarriageExample.createCriteria();
        criteria.andTripCodeEqualTo(code);
        criteria.andIndexEqualTo(index);
        return trainCarriageMapper.countByExample(TrainCarriageExample);
    }

}
